package cn.kvchoi.proactor_aio.server.handlers;

import cn.kvchoi.proactor_aio.server.SessionStateAttachment;

import java.io.IOException;
import java.nio.channels.AsynchronousSocketChannel;
import java.nio.channels.CompletionHandler;

public class WriteCompletionHandler implements CompletionHandler<Integer, SessionStateAttachment> {

    private final AsynchronousSocketChannel socketChannel;

    public WriteCompletionHandler(AsynchronousSocketChannel socketChannel) {
        this.socketChannel = socketChannel;
    }

    @Override
    public void completed(Integer bytesWritten, SessionStateAttachment attachment) {
        try {
            System.out.println("Write Handler running on " + Thread.currentThread().getName());
            System.out.println("\n");
            socketChannel.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void failed(Throwable exc, SessionStateAttachment attachment) {
        // Handle write failure.....
    }

}